﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using AliveMetro.Utils;

namespace MyDemos.Demos.WebDemo
{
    public partial class WebDemo : PhoneApplicationPage
    {
        public WebDemo ()
        {
            this.InitializeComponent ();
        }

        private void cBrowser_Loaded (object sender, RoutedEventArgs e)
        {
            //http://176.34.23.105/amg/image/24hr_en.jpg

            cBrowser.IsScriptEnabled = true;

            cBrowser.Navigate (new Uri ("http://176.34.23.105/amg/getAD.php?adName=home"));
            cBrowser.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs> (cBrowser_Navigated);


            //  cBrowser.NavigateToString ("<html><head><meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0' /></head><body><img src=\"http://176.34.23.105/amg/image/24hr_en.jpg\" width=100% height=100% border=0/> </body></html>");

            WebBrowser b = (sender as WebBrowser);
            var aa = VisualTreeHelper.GetChild (b, 0);
            var aa1 = VisualTreeHelper.GetChild (aa, 0);
            var aa2 = VisualTreeHelper.GetChild (aa1, 0);
            var aa3 = VisualTreeHelper.GetChild (aa2, 0);
            var aa4 = VisualTreeHelper.GetChild (aa3, 0);
            var aa5 = VisualTreeHelper.GetChild (aa4, 0);
            var aa6 = VisualTreeHelper.GetChild (aa5, 0);

            (aa4 as Border).ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs> (WebDemo_ManipulationDelta);
            (aa4 as Border).ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs> (WebDemo_ManipulationCompleted);

        }

        void cBrowser_Navigated (object sender, System.Windows.Navigation.NavigationEventArgs e)
        {


            //aas	"<HTML><HEAD><TITLE>COEVO-home</TITLE>\r\n<SCRIPT type=text/javascript src=\"http://www.google-analytics.com/ga.js\" async=\"true\"></SCRIPT>\r\n\r\n<SCRIPT type=text/javascript src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js\"></SCRIPT>\r\n\r\n<SCRIPT type=text/javascript>\n                        var adurl = \"http://tw.zinio.com/browse/publications/single-issues.jsp?productId=52139966\";\n\t\t\tvar ADid = \"55\";\n\t\t\t$(document).ready(function () {\n\t\t\t\tinsertLog(1, 55);\n                                $(\"#log\").click(function () {\n                                        insertLog(2, 55); \n                                });\n                        });\n                        \n                        function insertLog(_type,_AD_id){\n                                $.ajax({ url: \"insertLog.php?type=\"+_type+\"&AD_id=\"+_AD_id });\n                        };\n\n\t\t\t\n         </SCRIPT>\r\n\r\n<STYLE>\n                        body{\n                                margin-top :0px;\n                                margin-left:0px;\n                                margin-right:0px;\n                                margin-bottom:0px;\n                        }\n                        \n                        .coevo{\n\t\t\t\tposition:absolute;\n                        \ttop:0px;\n                        \tright:0px;\n                        \tmargin-top :0px;\n                            \tmargin-right:0px;\n                        }\n                        \n                </STYLE>\r\n\r\n<SCRIPT type=text/javascript>\n\n  \t\tvar _gaq = _gaq || [];\n  \t\t_gaq.push(['_setAccount', 'UA-28321601-1']);\n  \t\t_gaq.push(['_trackPageview']);\n\n  \t\t(function() {\n    \t\t\tvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n    \t\t\tga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n    \t\t\tvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n  \t\t})();\n\t\t\n\t\tfunction recordOutboundLink(link, category, action) {\n    \t\t\t_gat._getTrackerByName()._trackEvent(category, action);\n    \t\t\tsetTimeout('document.location = \"' + link.href + '\"', 100);\n  \t\t}\n\t</SCRIPT>\r\n</HEAD>\r\n<BODY>\r\n<DIV class=coevo><A href=\"http://www.coevo.com.tw/ \"><IMG border=0 src=\"http://176.34.23.105/amg/image/coevo_logo.png\" width=25 height=\"100%\"></A></DIV><A id=log onclick=\"recordOutboundLink(this, 'BW-home-all', 'log.php');return false;\" href=\"http://tw.zinio.com/browse/publications/single-issues.jsp?productId=52139966\" jQuery17105907473212254788=\"1\"><IMG border=0 src=\"http://176.34.23.105/amg/image/android_zinio.gif\" width=\"100%\" height=\"100%\"></A> </BODY></HTML>"	string

        }

        void WebDemo_ManipulationCompleted (object sender, ManipulationCompletedEventArgs e)
        {
            if (e.FinalVelocities.ExpansionVelocity.X != 0.0 ||
                e.FinalVelocities.ExpansionVelocity.Y != 0.0)
                e.Handled = true;
        }

        void WebDemo_ManipulationDelta (object sender, ManipulationDeltaEventArgs e)
        {
            if (e.DeltaManipulation.Scale.X != 0.0 ||
                e.DeltaManipulation.Scale.Y != 0.0)
                e.Handled = true;
            if (e.DeltaManipulation.Translation.X != 0.0 ||
  e.DeltaManipulation.Translation.Y != 0.0)
                e.Handled = true;
        }

        private void cBrowser_LoadCompleted (object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            String _Result = cBrowser.SaveToString ();
            int index = _Result.IndexOf ("height=\"100%\"></A> </BODY></HTML>");

            String _SubString = _Result.Substring (0, index);
            String _ModifiedHtml = _SubString + "height=\"200\"></A> </BODY></HTML>";
            cBrowser.NavigateToString (_ModifiedHtml);

        }

    }
}